/******************************************************************************
*
* This file is part of FreeTrack (http://code.google.com/p/qtfreetrack).
* BY Willy Gardiol (willy@gardiol.org)
*
* FreeTrack is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* FreeTrack is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Foobar.  If not, see <http://www.gnu.org/licenses/>
*
******************************************************************************/

#include "ftwidgetlistitem.h"
#include "ftwidgetlist.h"
#include "ftvboxlayout.h"
#include "ftcontrols.h"

int FTWLI_base_id = 0;
FTWidgetListItem::FTWidgetListItem(FTWidgetList *parent, QWidget* i) :
    QWidget(parent)
{
    setAcceptDrops(true);
    item = i;
    item->setParent( this );
    item->show();
    item->installEventFilter( parent );
    foreach (QObject* child, item->children() )
    {
        QWidget* widget = dynamic_cast<QWidget*>(child);
        if ( widget )
            widget->installEventFilter( parent );
    }
    FTVBoxLayout* grid = new FTVBoxLayout();
    grid->addWidget( i );
    setLayout(grid);

    id = QString("ID-%1").arg( FTWLI_base_id++ );

    connect( this, SIGNAL(draggingHere(FTWidgetListItem*)),
             parent, SLOT(dragHere(FTWidgetListItem*)) );

    setSizePolicy( QSizePolicy::Preferred, QSizePolicy::Preferred );
}

void FTWidgetListItem::dragEnterEvent(QDragEnterEvent *event)
{
    if (event->mimeData()->hasFormat("text/plain"))
    {
        event->acceptProposedAction();
        emit draggingHere(this);
    }
}

void FTWidgetListItem::dropEvent(QDropEvent *event)
{
    event->acceptProposedAction();
}

QString FTWidgetListItem::getId()
{
    return id;
}

QWidget* FTWidgetListItem::getItem()
{
    return item;
}

QSize FTWidgetListItem::sizeHint() const
{
    int min_height = FTControls::defaultHeight();
    QSize size = item->sizeHint();
    if ( size.height() < min_height )
        size.setHeight( min_height );
    return size;
}

QSize FTWidgetListItem::minimumSizeHint() const
{
    int min_height = FTControls::defaultHeight();
    QSize size = item->minimumSizeHint();
    if ( size.height() < min_height )
        size.setHeight( min_height );
    return size;
}
